
package cn.edu.tsinghua.hpc.gsmtest;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    static private boolean DEBUG = true;

    static private String TAG = "Preference";

    static private String NOT_SET = "not set";

    static private String D_TN = "13800138000";// Default Value

    static private String D_CL = "1000";

    static private String D_CI = "20";

    static private String D_CT = "10";

    static private String D_MI = "9";

    static private String D_MT = "5";

    static private String D_MF = "0";

    static private Context mContext;

    EditTextPreference mTargetNumber;

    EditTextPreference mCallLength;

    EditTextPreference mCallInternal;

    EditTextPreference mCallTimes;

    EditTextPreference mMsgInternal;

    EditTextPreference mMsgTimes;

    ListPreference mMsgFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        mContext = this;
        mTargetNumber = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_target_number));
        mCallLength = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_call_length));
        mCallInternal = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_call_internal));
        mCallTimes = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_call_times));
        mMsgInternal = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_msg_internal));
        mMsgTimes = (EditTextPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_msg_times));
        mMsgFormat = (ListPreference) getPreferenceScreen().findPreference(
                getString(R.string.pref_msg_format));
        setDefaultValue();
        setSummary(getString(R.string.pref_target_number));
        setSummary(getString(R.string.pref_call_internal));
        setSummary(getString(R.string.pref_call_length));
        setSummary(getString(R.string.pref_call_times));
        setSummary(getString(R.string.pref_msg_format));
        setSummary(getString(R.string.pref_msg_internal));
        setSummary(getString(R.string.pref_msg_times));

    }

    private void setDefaultValue() {
        if (!getStringById(R.string.target_number).equals(NOT_SET))
            return;
        setDefaultStringById(R.string.pref_target_number, D_TN);
        setDefaultStringById(R.string.pref_call_internal, D_CI);
        setDefaultStringById(R.string.pref_call_length, D_CL);
        setDefaultStringById(R.string.pref_call_times, D_CT);
        setDefaultStringById(R.string.pref_msg_format, D_MF);
        setDefaultStringById(R.string.pref_msg_internal, D_MI);
        setDefaultStringById(R.string.pref_msg_times, D_MT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (DEBUG)
            Log.d(TAG, "in onResume");
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (DEBUG)
            Log.d(TAG, "in onPause");
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
                this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
                this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (DEBUG)
            Log.d(TAG, "in onSharedPreferenceChanged");
        setSummary(key);
    }

    private void setSummary(String key) {
        String value = getStringByName(key);
        // Let's do something a preference value changes
        if (key.equals(getString(R.string.pref_target_number))) {
            mTargetNumber.setSummary(this.getString(R.string.pref_target_number_summary, value));
        } else if (key.equals(getString(R.string.pref_call_length))) {
            mCallLength.setSummary(this.getString(R.string.pref_call_length_summary, value));
        } else if (key.equals(getString(R.string.pref_call_internal))) {
            mCallInternal.setSummary(this.getString(R.string.pref_call_internal_summary, value));
        } else if (key.equals(getString(R.string.pref_call_times))) {
            mCallTimes.setSummary(this.getString(R.string.pref_call_times_summary, value));
        } else if (key.equals(getString(R.string.pref_msg_internal))) {
            mMsgInternal.setSummary(this.getString(R.string.pref_msg_internal_summary, value));
        } else if (key.equals(getString(R.string.pref_msg_times))) {
            mMsgTimes.setSummary(this.getString(R.string.pref_msg_times_summary, value));
        } else if (key.equals(getString(R.string.pref_msg_format))) {
            mMsgFormat.setSummary(this.getString(R.string.pref_msg_format_summary, value));
        }
    }

    public static void init(Context ctx) {
        mContext = ctx;

    }

    public String getStringByName(String name) {
        try {
            return getPreferenceScreen().getSharedPreferences().getString(name, NOT_SET);
        } catch (Exception e) {
            return null;
        }

    }

    static public int getIntById(int id) {
        // if(DEBUG) Log.d(TAG,mContext.getString(id));
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        return Integer.parseInt(settings.getString(mContext.getString(id), "0"));
    }

    static public String getStringById(int id) {
        // if(DEBUG) Log.d(TAG,mContext.getString(id));
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        return settings.getString(mContext.getString(id), NOT_SET);
    }

    static public boolean getBooleanById(int id) {
        // if(DEBUG) Log.d(TAG,mContext.getString(id));
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        return settings.getBoolean(mContext.getString(id), false);
    }

    static public void setStringByName(String name, String value) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(name, value);
        editor.commit();
        return;
    }

    private void setDefaultStringById(int id, String value) {
        getPreferenceScreen().findPreference(mContext.getString(id)).setDefaultValue(value);
    }

    static public void setStringById(int id, String value) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(mContext.getString(id), value);
        editor.commit();
        return;
    }

}
